package com.qiye.pointsserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiye.pointsserver.common.result.Result;
import com.qiye.pointsserver.common.util.MD5Util;
import com.qiye.pointsserver.model.dto.UseRegrDto;
import com.qiye.pointsserver.model.dto.UserLoginDto;
import com.qiye.pointsserver.model.entity.User;
import com.qiye.pointsserver.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@Tag(name = "01.用户模块")
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    private MD5Util md5Util;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result reg(@RequestBody UseRegrDto useRegrDto){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, useRegrDto.getUsername());
        User user = userService.getOne(wrapper);
        if (user != null) {
            return Result.failed("用户名已存在");
        }
        String key = "验证码"+ useRegrDto.getEmail();
        Object value = redisTemplate.opsForValue().get(key);
        int code = value != null ? Integer.parseInt(value.toString()) : -1;  // -1 表示未获取到验证码

        if (code != useRegrDto.getCode()) {
            return Result.failed("验证码错误");
        }
        String salt = md5Util.getSalt();
        String password = md5Util.encrypt(useRegrDto.getPassword(),salt);
        User user1 = new User();
        user1.setRole(useRegrDto.getRole());
        user1.setUsername(useRegrDto.getUsername());
        user1.setPassword(password);
        user1.setSalt(salt);
        user1.setEmail(useRegrDto.getEmail());
        userService.save(user1);
        return Result.success("注册成功");
    }


    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, userLoginDto.getUsername());
        User user = userService.getOne(wrapper);
        if (user == null) {
            return Result.failed("用户名或密码错误");
        }
        String password = md5Util.encrypt(userLoginDto.getPassword(),user.getSalt());
        if (!password.equals(user.getPassword())) {
            return Result.failed("用户名或密码错误");
        }
        user.setSalt(null);
        user.setPassword(null);
        user.setUsername(null);
        return Result.success(user);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/forget")
    public Result forget(@RequestBody UseRegrDto useRegrDto){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, useRegrDto.getUsername());
        wrapper.eq(User::getEmail, useRegrDto.getEmail());
        User user = userService.getOne(wrapper);
        if (user == null) {
            return Result.failed("用户名或邮箱错误");
        }
        String key = "验证码"+ useRegrDto.getEmail();
        Object value = redisTemplate.opsForValue().get(key);
        int code = value != null ? Integer.parseInt(value.toString()) : -1;  // -1 表示未获取到验证码

        if (code != useRegrDto.getCode()) {
            return Result.failed("验证码错误");
        }
        String salt = md5Util.getSalt();
        String password = md5Util.encrypt(useRegrDto.getPassword(),salt);
        String password1 = md5Util.encrypt(user.getPassword(),user.getSalt());
        if (password1.equals(user.getPassword())) {
            return Result.failed("新密码不能与旧密码相同");
        }
        user.setPassword(password);
        user.setSalt(salt);
        user.setUpdateTime(LocalDateTime.now());
        userService.updateById(user);
        return Result.success("修改密码成功");
    }
}




















